setwd("E:\\5hmc_file\\2_5hmc_yjp_bam\\ASM")
dir1="./bayes_pvalue_beta0/"
dir2="./bayes_BF/"
group1=c("X2B_X1T","M8_M7","M6_M5","M2_M1","M48_M47","M50_M49","M28_M27","M30_M29","M26_M25","M35_M36","M18_M17","M20_M19","M22_M21","M40_M39")

i=1
fn1=paste0(dir1,group1[i],".bayes_p.txt")
file1=read.table(fn1,head=T,sep = "\t")
file1$unitID=paste(file1$chrom,file1$position,file1$ref,file1$var,sep=":")
file1=data.frame(unitID=file1$unitID,normal_bayes_beta0=file1$normal_bayes_beta0,normal_bayes_pvalue=file1$normal_bayes_pvalue,tumor_bayes_beta0=file1$tumor_bayes_beta0,tumor_bayes_pvalue=file1$tumor_bayes_pvalue)
file1$FDR1=p.adjust(file1$normal_bayes_pvalue,method = "BH")
file1$FDR2=p.adjust(file1$tumor_bayes_pvalue,method = "BH")
file=file1[file1[,6]<0.1&file1[,7]<0.1,]
file$pattern.DC.group=""
file[file$normal_bayes_beta0>0&file$tumor_bayes_beta0>0,]$pattern.DC.group="up"###pattern: upup
file[file$normal_bayes_beta0<0&file$tumor_bayes_beta0<0,]$pattern.DC.group="down"###pattern: downdown
file=file[!file$pattern.DC.group=="",]
file=data.frame(unitID=file$unitID,pattern.DC.group=file$pattern.DC.group)
names(file)=c("unitID",paste0("pattern.DC.",group1[i]))
rt=file
for(i in 2:6){
  fn1=paste0(dir1,group1[i],".bayes_p.txt")
  file1=read.table(fn1,head=T,sep = "\t")
  file1$unitID=paste(file1$chrom,file1$position,file1$ref,file1$var,sep=":")
  file1=data.frame(unitID=file1$unitID,normal_bayes_beta0=file1$normal_bayes_beta0,normal_bayes_pvalue=file1$normal_bayes_pvalue,tumor_bayes_beta0=file1$tumor_bayes_beta0,tumor_bayes_pvalue=file1$tumor_bayes_pvalue)
  file1$FDR1=p.adjust(file1$normal_bayes_pvalue,method = "BH")
  file1$FDR2=p.adjust(file1$tumor_bayes_pvalue,method = "BH")
  file=file1[file1[,6]<0.1&file1[,7]<0.1,]
  file$pattern.DC.group=""
  file[file$normal_bayes_beta0>0&file$tumor_bayes_beta0>0,]$pattern.DC.group="up"###pattern: upup
  file[file$normal_bayes_beta0<0&file$tumor_bayes_beta0<0,]$pattern.DC.group="down"###pattern: downdown
  file=file[!file$pattern.DC.group=="",]
  file=data.frame(unitID=file$unitID,pattern.DC.group=file$pattern.DC.group)
  names(file)=c("unitID",paste0("pattern.DC.",group1[i]))
  rt=merge(rt,file,by="unitID",all = T)
}

for(i in 7:10){
  fn1=paste0(dir1,group1[i],".bayes_p.txt")
  file1=read.table(fn1,head=T,sep = "\t")
  file1$unitID=paste(file1$chrom,file1$position,file1$ref,file1$var,sep=":")
  file1=data.frame(unitID=file1$unitID,normal_bayes_beta0=file1$normal_bayes_beta0,normal_bayes_pvalue=file1$normal_bayes_pvalue,tumor_bayes_beta0=file1$tumor_bayes_beta0,tumor_bayes_pvalue=file1$tumor_bayes_pvalue)
  file1$FDR1=p.adjust(file1$normal_bayes_pvalue,method = "BH")
  file1$FDR2=p.adjust(file1$tumor_bayes_pvalue,method = "BH")
  file1.normaldata=data.frame(unitID=file1$unitID,normal_bayes_beta0=file1$normal_bayes_beta0,FDR1=file1$FDR1)
  file1.tumordata=data.frame(unitID=file1$unitID,tumor_bayes_beta0=file1$tumor_bayes_beta0,FDR2=file1$FDR2)
  
  file1.normaldata=file1.normaldata[file1.normaldata$FDR1<0.1,]
  file1.normaldata$pattern.CC.group=ifelse(file1.normaldata$normal_bayes_beta0>0,"up","down")
  file1.tumordata=file1.tumordata[file1.tumordata$FDR2<0.1,]
  file1.tumordata$pattern.CC.group=ifelse(file1.tumordata$tumor_bayes_beta0>0,"up","down")
  file1.normaldata=data.frame(unitID=file1.normaldata$unitID,pattern.CC.group=file1.normaldata$pattern.CC.group)
  file1.tumordata=data.frame(unitID=file1.tumordata$unitID,pattern.CC.group=file1.tumordata$pattern.CC.group)
  
  names(file1.normaldata)=c("unitID",paste0("pattern.CC.group.",unlist(strsplit(group1[i],"_"))[1]))
  names(file1.tumordata)=c("unitID",paste0("pattern.CC.group.",unlist(strsplit(group1[i],"_"))[2]))
  rt=merge(rt,file1.normaldata,by="unitID",all = T)
  rt=merge(rt,file1.tumordata,by="unitID",all = T)
}

for(i in 11:14){
  fn1=paste0(dir1,group1[i],".bayes_p.txt")
  file1=read.table(fn1,head=T,sep = "\t")
  file1$unitID=paste(file1$chrom,file1$position,file1$ref,file1$var,sep=":")
  file1=data.frame(unitID=file1$unitID,normal_bayes_beta0=file1$normal_bayes_beta0,normal_bayes_pvalue=file1$normal_bayes_pvalue,tumor_bayes_beta0=file1$tumor_bayes_beta0,tumor_bayes_pvalue=file1$tumor_bayes_pvalue)
  file1$FDR1=p.adjust(file1$normal_bayes_pvalue,method = "BH")
  file1$FDR2=p.adjust(file1$tumor_bayes_pvalue,method = "BH")
  file1.normaldata=data.frame(unitID=file1$unitID,normal_bayes_beta0=file1$normal_bayes_beta0,FDR1=file1$FDR1)
  file1.tumordata=data.frame(unitID=file1$unitID,tumor_bayes_beta0=file1$tumor_bayes_beta0,FDR2=file1$FDR2)
  
  file1.normaldata=file1.normaldata[file1.normaldata$FDR1<0.1,]
  file1.normaldata$pattern.HC.group=ifelse(file1.normaldata$normal_bayes_beta0>0,"up","down")
  file1.tumordata=file1.tumordata[file1.tumordata$FDR2<0.1,]
  file1.tumordata$pattern.HC.group=ifelse(file1.tumordata$tumor_bayes_beta0>0,"up","down")
  file1.normaldata=data.frame(unitID=file1.normaldata$unitID,pattern.HC.group=file1.normaldata$pattern.HC.group)
  file1.tumordata=data.frame(unitID=file1.tumordata$unitID,pattern.HC.group=file1.tumordata$pattern.HC.group)
  
  names(file1.normaldata)=c("unitID",paste0("pattern.HC.group.",unlist(strsplit(group1[i],"_"))[1]))
  names(file1.tumordata)=c("unitID",paste0("pattern.HC.group.",unlist(strsplit(group1[i],"_"))[2]))
  rt=merge(rt,file1.normaldata,by="unitID",all = T)
  rt=merge(rt,file1.tumordata,by="unitID",all = T)
}
library(tidyr)

file=rt
file$group.DC.num=rowSums(!is.na(file[,2:7]),na.rm = T)
file$group.CC.HC.num=rowSums(!is.na(file[,8:23]),na.rm = T)
file$pattern.num=2*file$group.DC.num+file$group.CC.HC.num

file$pattern.no.dupl=""
for (i in 1:dim(file)[1]) {
  str1=as.character(c(unlist(as.matrix(file[i,2:23]))))
  str1=unique(str1[!is.na(str1)])
  str1=paste(str1,sep=":",collapse = ":")
  file[i,"pattern.no.dupl"]=str1
}

file$group.CC=rowSums(!is.na(file[,8:15]),na.rm = T)
file$group.HC=rowSums(!is.na(file[,16:23]),na.rm = T)

file$pattern.no.dupl.DC=""
for (i in 1:dim(file)[1]) {
  str1=as.character(c(unlist(as.matrix(file[i,2:7]))))
  str1=unique(str1[!is.na(str1)])
  str1=paste(str1,sep=":",collapse = ":")
  file[i,"pattern.no.dupl.DC"]=str1
}

file$pattern.no.dupl.CC=""
for (i in 1:dim(file)[1]) {
  str1=as.character(c(unlist(as.matrix(file[i,8:15]))))
  str1=unique(str1[!is.na(str1)])
  str1=paste(str1,sep=":",collapse = ":")
  file[i,"pattern.no.dupl.CC"]=str1
}

file$pattern.no.dupl.HC=""
for (i in 1:dim(file)[1]) {
  str1=as.character(c(unlist(as.matrix(file[i,16:23]))))
  str1=unique(str1[!is.na(str1)])
  str1=paste(str1,sep=":",collapse = ":")
  file[i,"pattern.no.dupl.HC"]=str1
}
dir.create("./20201124")
write.csv(file,"./20201124/same.direction.dowdown.upup.csv",quote=F,row.names = F)



